package com.ec.au.controller;

import com.ec.au.domain.dto.UserLoginDTO;
import com.ec.common.domain.Result;
import com.ec.au.domain.dto.UserRegisterFormDTO;
import com.ec.au.domain.dto.UserUpdateDTO;
import com.ec.au.domain.po.User;
import com.ec.au.domain.vo.TokenVO;
import com.ec.au.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/user/login")
    public Result<TokenVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        return Result.ok(userService.login(userLoginDTO));
    }

    @PostMapping("/user/logout")
    public Result<Void> logout(){
        if(userService.logout()){
            return Result.ok();
        }else{
            return Result.error("注销失败");
        }
    }

    @PostMapping("/user/register")
    public Result<Void> register(@RequestBody UserRegisterFormDTO userRegisterFormDTO) {
        if(userService.register(userRegisterFormDTO)){
            return Result.ok();
        }else{
            return Result.error("注册失败");
        }
    }

    @PostMapping("/user/updatepwd")
    public Result updatePwd( @RequestBody UserUpdateDTO userUpdateDTO) {
        if(userService.updatePassword(userUpdateDTO)){
            return Result.ok();
        }else{
            return Result.error("修改密码失败");
        }
    }

}
